﻿EntityEarthquake = Eg.Entity.extend({
  _wmDrawBox: true,
  _wmBoxColor: 'rgba(80, 130, 170, 0.7)',
  size: {
    x: 8,
    y: 8
  },
  duration: 1,
  strength: 8,
  screen: {
    x: 0,
    y: 0
  },
  sound: new Eg.Sound('Resource/earthquake.mp3', false),
  quakeTimer: null,
  init: function (x, y, settings) {
    this.quakeTimer = new Eg.Timer();
    this.parent(x, y, settings);
  },
  triggeredBy: function (entity, trigger) {
    this.quakeTimer.set(this.duration);
    if (this.sound) {
      this.sound.play();
    }
  },
  update: function () {
    var delta = this.quakeTimer.delta();
    if (delta < -0.1) {
      var s = this.strength * Math.pow(-delta / this.duration, 2);
      if (s > 0.5) {
        Eg.game.screen.x += Math.random().map(0, 1, -s, s);
        Eg.game.screen.y += Math.random().map(0, 1, -s, s);
      }
    }
  }
});